package com.tubruk.todo;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;

import com.tubruk.todo.activity.R;
import com.tubruk.todo.activity.TodoActivity;
import com.tubruk.todo.database.TodoAdapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class TaskAdapter extends ArrayAdapter<TaskContent>{
	
	private ArrayList<TaskContent> items;
	
	private static String TEXT_DUE_DATE = "Due Date: ";
	private static String TEXT_DUE_DATE_NONE = "Due Date: <None>";
	
	private OnClickHandler onClickHandler = null;
	private OnTickHandler onTickHandler = null;
	private OnDropTaskHandler onDropTaskHandler = null;
	private OnClickSubTaskHandler onClickSubTaskHandler = null;
	
	private TodoActivity activity = null;
	
	public ArrayList<TaskContent> getItems(){
		return items;
	}
		
	private class OnClickHandler implements View.OnClickListener {

		public void onClick(View v) {		
			activity.setSelectedItemId((Integer) v.getTag());
			activity.showDialog(TodoActivity.LISTMENU_DIALOG_ID);
		}
		
	}

	
	private class OnClickSubTaskHandler implements View.OnClickListener {

		public void onClick(View v) {		
			activity.setSelectedItemId((Integer) v.getTag());
			activity.showDialog(TodoActivity.LISTMENU_SUBTASK_DIALOG_ID);
		}
		
	}

	private class OnDropTaskHandler implements View.OnClickListener {
		public void onClick(View v){
			TaskContent o = (TaskContent) v.getTag();
			
			if (o.isExpanded()){
				activity.closeTask(o.getId());
				o.setExpanded(false);
			}
			else {
				activity.dropTask(o.getId());
				o.setExpanded(true);
			}
		}
	}
	
	private class OnTickHandler implements View.OnClickListener {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			CheckBox cb = (CheckBox) v;
			TaskContent o = (TaskContent) v.getTag();
			
			int done = TaskContent.DONE_NO;
			if (cb.isChecked()){
				done = TaskContent.DONE_YES;		
			}
			
			//set children
			if (o.getChild() != null) {
				int size = o.getChild().size();
				if (size > 0) {
					TodoAdapter.updateParentComplete(activity
							.getContentResolver(), done, o.getId());
					for (int i = 0; i < size; i++) {
						o.getChild().get(i).setDone(done);
					}
				}
				
				//need to update UI if child task is expanded
				notifyDataSetChanged();
			}
			//set parent
			o.setDone(done);
			TodoAdapter.updateComplete(activity.getContentResolver(), done, o.getId());
		}
	}
	
	public TaskAdapter(Context context, int textViewResourceId, ArrayList<TaskContent> items) {
        super(context, textViewResourceId, items);
        this.activity = (TodoActivity) getContext();
        this.items = items;
        
        onClickHandler = new OnClickHandler();
        onTickHandler = new OnTickHandler();
        onDropTaskHandler = new OnDropTaskHandler();
        
        onClickSubTaskHandler = new OnClickSubTaskHandler();
	}
	
	@Override
    public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.dataview, null);
            }
            TaskContent o = items.get(position);
            if (o != null) {
        		v.setTag(o.getId());
            	if (o.getParentId() == 0) {
            		//no parent id: means it is not a sub task
            		v.setOnClickListener(onClickHandler);
            		v.setBackgroundColor(Global.COLOR_TASK);
            	}else {
            		v.setOnClickListener(onClickSubTaskHandler);
            		v.setBackgroundColor(Global.COLOR_SUBTASK);
            	}
            	
        		ImageButton dropTask = (ImageButton) v.findViewById(R.id.DropTask);
        		if (dropTask != null){
        			if (o.getChild() != null){
        			//dropTask.setId(id);
     //   				dropTask = (ImageView) v.findViewById(R.id.DropTask);

        				if (!o.isExpanded()){
        					dropTask.setImageResource(R.drawable.expander_ic_minimized);
        				}else {
        					dropTask.setImageResource(R.drawable.expander_ic_maximized);
        				}
        				dropTask.setVisibility(View.VISIBLE);

        				dropTask.setTag(o);
        				dropTask.setOnClickListener(onDropTaskHandler);
        			}else {

        				dropTask.setOnClickListener(null);
        				if (o.getParentId() == 0){
       // 					dropTask = (ImageView) v.findViewById(R.id.DropTask);
        					dropTask.setImageResource(R.drawable.expander_ic_maximized);
            				dropTask.setVisibility(View.VISIBLE);
        				}else {
        					dropTask.setVisibility(View.INVISIBLE);
        				}
        			}
        		}
        		
        		TextView task = (TextView) v.findViewById(R.id.task);
        		if (task != null){
        			task.setText(o.getTitle());
        		}
        		
        		Date taskDueDate = o.getDueDate();
        		TextView dueDate = (TextView) v.findViewById(R.id.dueDate);
    			
        		if (dueDate != null) {
        			if (taskDueDate != null){
        				dueDate.setText(TaskAdapter.TEXT_DUE_DATE + 
        						DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT)
        						.format(taskDueDate.getTime())
        				);
        			}else {
        				dueDate.setText(TaskAdapter.TEXT_DUE_DATE_NONE);
        			}	
        		}
        		
        		int priority = o.getPriority();
        		ImageView priorityView = (ImageView) v.findViewById(R.id.priorityView);
        		if (priorityView != null){
        			int bgColor = Global.PRIORITY_COLOR_NONE;
	        		if (priority != TaskContent.PRIORITY_NONE){
	        			if (priority == TaskContent.PRIORITY_LOW) { 
	    						bgColor = Global.PRIORITY_COLOR_LOW;
	    				}else if (priority == TaskContent.PRIORITY_MEDIUM){ 
	    						bgColor = Global.PRIORITY_COLOR_MEDIUM;
	    				}else if (priority == TaskContent.PRIORITY_HIGH) { 
	    						bgColor = Global.PRIORITY_COLOR_HIGH; 
	    				}
	        		}
            		priorityView.setBackgroundColor(bgColor);
            	}
			
        		CheckBox doneCB = (CheckBox) v.findViewById(R.id.doneCB);
        		if (doneCB != null ){
        			doneCB = (CheckBox) v.findViewById(R.id.doneCB);
        			
        			int hasDone = o.getDone();
        			if (hasDone == TaskContent.DONE_YES){
        				doneCB.setChecked(true);
        			}else {
        				doneCB.setChecked(false);
        			}
        			doneCB.setOnClickListener(onTickHandler);
        			doneCB.setTag(o);
        		}
        	}
            return v;
    }

}
